{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "99878f89-5a41-4fea-bba1-5dd8f51c1641",
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3ed2f4b8-3753-4ae3-b13e-77903c026b08",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个协程， 直接调用不会执行， 而是生成一个协程对象\n",
    "# class 'coroutine'\n",
    "# my_coro() 返回的是一个 coroutine object\n",
    "async def my_coro():\n",
    "    print('Hello, World!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cdd650ff-71c3-453b-a69b-4c87832ad660",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_one(x: int) -> int:\n",
    "    return x + 1\n",
    "\n",
    "async def coroutine_add_one(x: int) -> int:\n",
    "    return x + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "da880da3-5f16-45b4-aac1-f576f00ca2ab",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "asyncio.run() cannot be called from a running event loop",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 16\u001b[0m\n\u001b[0;32m     13\u001b[0m     \u001b[38;5;28mprint\u001b[39m(result)\n\u001b[0;32m     15\u001b[0m \u001b[38;5;66;03m# 如何运行 coroutine object\u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m \u001b[43masyncio\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\APPS\\conda\\Lib\\asyncio\\runners.py:190\u001b[0m, in \u001b[0;36mrun\u001b[1;34m(main, debug, loop_factory)\u001b[0m\n\u001b[0;32m    161\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Execute the coroutine and return the result.\u001b[39;00m\n\u001b[0;32m    162\u001b[0m \n\u001b[0;32m    163\u001b[0m \u001b[38;5;124;03mThis function runs the passed coroutine, taking care of\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    186\u001b[0m \u001b[38;5;124;03m    asyncio.run(main())\u001b[39;00m\n\u001b[0;32m    187\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    188\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m events\u001b[38;5;241m.\u001b[39m_get_running_loop() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m    189\u001b[0m     \u001b[38;5;66;03m# fail fast with short traceback\u001b[39;00m\n\u001b[1;32m--> 190\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[0;32m    191\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124masyncio.run() cannot be called from a running event loop\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    193\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m Runner(debug\u001b[38;5;241m=\u001b[39mdebug, loop_factory\u001b[38;5;241m=\u001b[39mloop_factory) \u001b[38;5;28;01mas\u001b[39;00m runner:\n\u001b[0;32m    194\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m runner\u001b[38;5;241m.\u001b[39mrun(main)\n",
      "\u001b[1;31mRuntimeError\u001b[0m: asyncio.run() cannot be called from a running event loop"
     ]
    }
   ],
   "source": [
    "async def main():\n",
    "    # await 挂起当前协程，等待 my_coro() 执行完毕\n",
    "    await my_coro()\n",
    "    function_result = add_one(1)\n",
    "    coroutine_result =  coroutine_add_one(1)\n",
    "\n",
    "    print(f'function_result: {function_result} and the type is {type(function_result)}')\n",
    "    print(f'coroutine_result: {coroutine_result} and the type is {type(coroutine_result)}')\n",
    "    # function_result: 2 and the type is <class 'int'>\n",
    "    # coroutine_result: <coroutine object coroutine_add_one at 0x000001C5139C7A00> and the type is <class 'coroutine'>\n",
    "\n",
    "    result = await coroutine_result\n",
    "    print(result)\n",
    "\n",
    "# 如何运行 coroutine object\n",
    "asyncio.run(main())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d43de711-f51e-45d8-86de-1a434b74f067",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
